//
//  CLubDetailTableViewCell.m
//  TasteEnjoy
//
//  Created by XuRui on 16/4/27.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "CLubDetailTableViewCell.h"

@interface CLubDetailTableViewCell ()

@property (nonatomic, retain) BaseImageView *imgUserPhoto;  /**< 用户头像 */
@property (nonatomic, retain) BaseLabel *labelUserName; /**< 用户名 */
@property (nonatomic, retain) BaseLabel *labelTime; /**< 时间 */

@property (nonatomic, retain) BaseView *viewBackGround; /**< 白色背景 */

@property (nonatomic, retain) BaseLabel *labelTitle; /**< 题目 */
@property (nonatomic, retain) BaseImageView *imgPhoto;  /**< 图片 */


@property (nonatomic, retain) BaseImageView *imgComment;  /**< 评论图标 */
@property (nonatomic, retain) BaseLabel *labelCommentCount; /**< 评论数 */

@property (nonatomic, retain) BaseImageView *imgZan; /**< 赞图标 */

@property (nonatomic, retain) BaseLabel *labelZan; /**< 赞 */

@property (nonatomic, retain) BaseImageView *imgAlert; /**< 弹出图标 */

@property (nonatomic, retain) BaseView *viewLine; /**< 线 */
@property (nonatomic, retain) BaseLabel *labelComment; /**< 用户名 + 评论 */
@property (nonatomic, retain) BaseLabel *labelSeeAll; /**< 查看所有回应 */

@end

@implementation CLubDetailTableViewCell

#pragma mark - ------------ 内存管理 ---------------

- (void)dealloc
{
    [_imgUserPhoto release];
    [_labelUserName release];
    [_labelTime release];
    [_labelTitle release];
    [_imgPhoto release];
    [_imgComment release];
    [_labelCommentCount release];
    [_imgZan release];
    [_labelZan release];
    [_imgAlert release];
    [_viewBackGround release];
    [_viewLine release];
    [_labelComment release];
    [_labelSeeAll release];
    [_model release];
    [super dealloc];
}

#pragma mark - ------------ 初始化 ---------------
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.contentView.dk_backgroundColorPicker = DKColorWithColors(GrayLight,[UIColor lightGrayColor]);
        [self creatSubViews];
    }
    return self;


}

#pragma mark - ------------ 创建子视图 ---------------
- (void)creatSubViews
{
    self.imgUserPhoto = [[BaseImageView alloc]init];
    [self.contentView addSubview:_imgUserPhoto];
    _imgUserPhoto.layer.cornerRadius = 20;
    _imgUserPhoto.clipsToBounds = YES;
    
    
    self.labelUserName = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelUserName];
    _labelUserName.textColor = [UIColor colorWithRed:0.04 green:0.64 blue:0.85 alpha:1.00];
    _labelUserName.font = [UIFont systemFontOfSize:13 weight:15];
    
    
    self.labelTime = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelTime];
    _labelTime.font = [UIFont systemFontOfSize:15];
/** 夜间 */
    _labelTime.dk_textColorPicker = DKColorWithColors([UIColor lightGrayColor],[UIColor grayColor]);
    
    self.viewBackGround = [[BaseView alloc]init];
/** 夜间 */
    _viewBackGround.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor grayColor]);
    [self.contentView addSubview:_viewBackGround];
    
    
    self.labelTitle = [[BaseLabel alloc]init];
    [self.viewBackGround addSubview:_labelTitle];
    _labelTitle.numberOfLines = 0;
    _labelTitle.font =[UIFont systemFontOfSize:15];
/** 夜间 */
    _labelTitle.dk_textColorPicker = DKColorWithColors([UIColor grayColor],[UIColor lightGrayColor]);
    
    
    
    self.imgPhoto = [[BaseImageView alloc]init];
    [self.viewBackGround addSubview:_imgPhoto];
    _imgPhoto.contentMode = UIViewContentModeScaleAspectFill;
    _imgPhoto.clipsToBounds = YES;
    
    
    
    self.imgComment = [[BaseImageView alloc]init];
    [self.viewBackGround addSubview:_imgComment];
    
    
    
    self.labelCommentCount = [[BaseLabel alloc]init];
    [self.viewBackGround addSubview:_labelCommentCount];
    _labelCommentCount.textColor = [UIColor lightGrayColor];
    _labelCommentCount.font = [UIFont systemFontOfSize:13];
    
    
    
    self.imgZan = [[BaseImageView alloc]init];
    [self.viewBackGround addSubview:_imgZan];
    
    self.labelZan = [[BaseLabel alloc]init];
    [self.viewBackGround addSubview:_labelZan];
    _labelZan.textColor = [UIColor lightGrayColor];
    _labelZan.font = [UIFont systemFontOfSize:13];
    
    
    self.imgAlert = [[BaseImageView alloc]init];
    [self.viewBackGround addSubview:_imgAlert];
    
    
    self.viewLine = [[BaseView alloc]init];
    [self.viewBackGround addSubview:_viewLine];
    
    
    self.labelComment = [[BaseLabel alloc]init];
    [self.viewBackGround addSubview:_labelComment];
    _labelComment.numberOfLines = 0;
    _labelComment.font = [UIFont systemFontOfSize:15];
/** 夜间 */
    _labelComment.dk_textColorPicker = DKColorWithColors([UIColor grayColor],[UIColor lightGrayColor]);
    
    
    
    self.labelSeeAll = [[BaseLabel alloc]init];
    [self.viewBackGround addSubview:_labelSeeAll];
    _labelSeeAll.textColor = [UIColor lightGrayColor];
    _labelSeeAll.font = [UIFont systemFontOfSize:13];
    
    _viewLine.backgroundColor = [UIColor lightGrayColor];
    
    
    [_imgUserPhoto release];
    [_labelUserName release];
    [_labelTime release];
    [_labelTitle release];
    [_imgPhoto release];
    [_imgComment release];
    [_labelCommentCount release];
    [_imgZan release];
    [_labelZan release];
    [_imgAlert release];
    [_viewBackGround release];
    [_viewLine release];
    [_labelComment release];
    [_labelSeeAll release];

    
    
//    _imgUserPhoto.backgroundColor = [UIColor yellowColor];
//    _labelUserName.backgroundColor = [UIColor redColor];
//    _labelTime.backgroundColor = [UIColor orangeColor];
//    
//    _labelTitle.backgroundColor = [UIColor yellowColor];
//    _imgPhoto.backgroundColor = [UIColor redColor];
//    
//    _imgZan.backgroundColor = [UIColor redColor];
//    _labelZan.backgroundColor = [UIColor grayColor];
//    _imgComment.backgroundColor = [UIColor redColor];
//    _labelCommentCount.backgroundColor =[ UIColor grayColor];
    
//    _imgAlert.backgroundColor = [UIColor redColor];
    
//    _labelComment.backgroundColor = [UIColor yellowColor];
//    _labelSeeAll.backgroundColor = [UIColor redColor];

}


#pragma mark - ------------ 设置大小 ---------------
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self transNo];
    
    [_imgUserPhoto mas_makeConstraints:^(MASConstraintMaker *make) {
       
      
        make.top.mas_equalTo(self.contentView).offset(10);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    
    [_labelUserName mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_imgUserPhoto);
        make.left.mas_equalTo(_imgUserPhoto.mas_right).offset(10);
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(30);
    }];
    
    [_labelTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imgUserPhoto);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.height.mas_equalTo(_labelUserName);
    }];
    
    CGFloat h = [[self class]heightWithText:_model.content];
    CGFloat h1 = [[self class]heightWithText:_labelComment.text];
    
/**< 既有图片又有评论 */
    if (_model.path != nil && _model.replayContent != nil) {
        
   
 
    [_viewBackGround mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_labelUserName.mas_bottom).offset(5);
        make.left.mas_equalTo(_imgUserPhoto.mas_right).offset(5);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.height.mas_equalTo(h + h1 + 271);
    }];
    
    
    [_labelTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
    
        make.left.top.mas_equalTo(_viewBackGround).offset(10);
        make.right.mas_equalTo(_viewBackGround).offset(-10);
        make.height.mas_equalTo(h);
    }];
    
    _imgPhoto.hidden = NO;

    [_imgPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_labelTitle.mas_bottom).offset(10);
        make.left.mas_equalTo(_viewBackGround).offset(10);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(120);
        
    }];
      
        
    [_imgComment mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_viewBackGround).offset(15);
        make.top.mas_equalTo(_imgPhoto.mas_bottom).offset(20);
        make.height.width.mas_equalTo(25);
        
    }];
    
    [_labelCommentCount mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_imgComment);
        make.left.mas_equalTo(_imgComment.mas_right).offset(5);
        make.height.mas_equalTo(_imgComment);
        make.width.mas_equalTo(30);
    }];
    
    [_imgZan mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_imgComment);
        make.left.mas_equalTo(_labelCommentCount.mas_right).offset(60);
        make.width.height.mas_equalTo(_imgComment);
      
    }];
    
    [_labelZan mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_imgComment);
        make.left.mas_equalTo(_imgZan.mas_right).offset(5);
        make.height.mas_equalTo(_imgComment);
        make.width.mas_equalTo(_labelCommentCount);

    }];
    
    [_imgAlert mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_imgComment);
        make.right.mas_equalTo(_viewBackGround).offset(-10);
        make.width.height.mas_equalTo(_imgComment);
       
    }];
    
    _viewLine.hidden = NO;
    [_viewLine mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.mas_equalTo(_viewBackGround).offset(0);
        make.top.mas_equalTo(_imgComment.mas_bottom).offset(10);
        make.height.mas_equalTo(1);
    
    }];
    
    _labelComment.hidden = NO;
    [_labelComment mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_viewLine.mas_bottom).offset(20);
        make.left.mas_equalTo(_viewBackGround).offset(10);
        make.right.mas_equalTo(_viewBackGround).offset(-10);
        make.height.mas_equalTo(h1);
       
    }];
    
    _labelSeeAll.hidden = NO;
    [_labelSeeAll mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_labelComment.mas_bottom).offset(10);
        make.left.mas_equalTo(_viewBackGround).offset(10);
        make.right.mas_equalTo(_viewBackGround).offset(-10);
        make.height.mas_equalTo(30);
 
    }];
   
    
    /** 图片为空 评论为空 */
    }
    else if(_model.path == nil  &&  _model.replayContent == nil ){
    
        [_viewBackGround mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelUserName.mas_bottom).offset(5);
            make.left.mas_equalTo(_imgUserPhoto.mas_right).offset(5);
            make.right.mas_equalTo(self.contentView).offset(-10);
            make.height.mas_equalTo(h + 60);
        }];
        
        
        [_labelTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.top.mas_equalTo(_viewBackGround).offset(10);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.height.mas_equalTo(h);
        }];
        
 
        _imgPhoto.hidden = YES;
        
        
        [_imgComment mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(_viewBackGround).offset(15);
            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(20);
            make.height.width.mas_equalTo(25);
        }];
        
        [_labelCommentCount mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgComment.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(30);
        }];
        
        [_imgZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_labelCommentCount.mas_right).offset(60);
            make.width.height.mas_equalTo(_imgComment);
        }];
        
        [_labelZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgZan.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(_labelCommentCount);
        }];
        
        [_imgAlert mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.width.height.mas_equalTo(_imgComment);
        }];
        
        _viewLine.hidden = YES;
        
        _labelComment.hidden = YES;
        
        _labelSeeAll.hidden = YES;
    
        
/** 图片为空 有评论 */
    }
    else if (_model.path == nil && _model.replayContent != nil){
        
        [_viewBackGround mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelUserName.mas_bottom).offset(5);
            make.left.mas_equalTo(_imgUserPhoto.mas_right).offset(5);
            make.right.mas_equalTo(self.contentView).offset(-10);
            make.height.mas_equalTo(h + h1 + 141);
        }];
        
        [_labelTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.top.mas_equalTo(_viewBackGround).offset(10);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.height.mas_equalTo(h);
        }];
        

        _imgPhoto.hidden = YES;
        
        
        [_imgComment mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(_viewBackGround).offset(15);
            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(20);
            make.height.width.mas_equalTo(25);
            
        }];
        
        [_labelCommentCount mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgComment.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(30);
        }];
        
        [_imgZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_labelCommentCount.mas_right).offset(60);
            make.width.height.mas_equalTo(_imgComment);
        }];
        
        [_labelZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgZan.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(_labelCommentCount);
        }];
        
        [_imgAlert mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.width.height.mas_equalTo(_imgComment);
            
        }];
        
        _viewLine.hidden = NO;
        [_viewLine mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.right.mas_equalTo(_viewBackGround).offset(0);
            make.top.mas_equalTo(_imgComment.mas_bottom).offset(10);
            make.height.mas_equalTo(1);
            
        }];
        
        _labelComment.hidden = NO;
        [_labelComment mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_viewLine.mas_bottom).offset(20);
            make.left.mas_equalTo(_viewBackGround).offset(10);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.height.mas_equalTo(h1);
            
        }];
        
        _labelSeeAll.hidden = NO;
        [_labelSeeAll mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelComment.mas_bottom).offset(10);
            make.left.mas_equalTo(_viewBackGround).offset(10);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.height.mas_equalTo(30);
            
        }];
 /** 图片有 无评论 */
    }
    else if (_model.path != nil && _model.replayContent == nil){
    
        
        [_viewBackGround mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelUserName.mas_bottom).offset(5);
            make.left.mas_equalTo(_imgUserPhoto.mas_right).offset(5);
            make.right.mas_equalTo(self.contentView).offset(-10);
            make.height.mas_equalTo(h + 190);
        }];
        
        
        [_labelTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.top.mas_equalTo(_viewBackGround).offset(10);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.height.mas_equalTo(h);
        }];
        
        _imgPhoto.hidden = NO;
        [_imgPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
        
            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(10);
            make.left.mas_equalTo(_viewBackGround).offset(10);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(120);
        
        }];
        
        [_imgComment mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(_viewBackGround).offset(15);
            make.top.mas_equalTo(_imgPhoto.mas_bottom).offset(20);
            make.height.width.mas_equalTo(25);
            
        }];
        
        [_labelCommentCount mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgComment.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(30);
        }];
        
        [_imgZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_labelCommentCount.mas_right).offset(60);
            make.width.height.mas_equalTo(_imgComment);
        }];
        
        [_labelZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgZan.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(_labelCommentCount);
        }];
        
        [_imgAlert mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.width.height.mas_equalTo(_imgComment);
            
        }];
    
        _viewLine.hidden = YES;
    
        _labelComment.hidden = YES;
        
        _labelSeeAll.hidden = YES;
   
    }
}

- (void)setModel:(ClubModel *)model
{

    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    [_imgUserPhoto sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:PLACEHOLDER];
    _labelUserName.text = model.username;
    
    /** 时间戳转换 */
    _labelTime.text = [[self class]howlongToCurrntTime:model.add_datetime_ts.stringValue];
    _labelTitle.text = model.content;
    
    [_imgPhoto sd_setImageWithURL:[NSURL URLWithString:model.path] placeholderImage:PLACEHOLDER];

    _imgComment.image = [UIImage imageNamed:@"ping"];

    _labelCommentCount.text = model.reply_count.stringValue;
    
    _imgZan.image = [UIImage imageNamed:@"zan"];
    _labelZan.text = model.like_count.stringValue;
    
    _imgAlert.image = [UIImage imageNamed:@"gengduo"];
    
    _labelComment.text = [[model.replayUsername stringByAppendingString:@":   "] stringByAppendingString:model.replayContent];
   
    _labelSeeAll.text = @"查看所有回应";
}

#pragma mark - ------------ 文本高度自适应 ---------------
+ (CGFloat)heightWithText:(NSString *)text
{
    
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 80, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil];
    
    return rect.size.height;
    
}

#pragma mark - ------------ 时间转换 ---------------
+ (NSString *)howlongToCurrntTime:(NSString *)time
{
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time.doubleValue];
    
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc]init];
    [dateFomatter setDateFormat:@"M月dd日 HH:mm"];
    
    //1970  转至今的时间
    NSString *fomatterString = [dateFomatter stringFromDate:date];
    
    [dateFomatter release];
    
    return fomatterString;
    
    
}



- (void)transNo
{
    _viewBackGround.translatesAutoresizingMaskIntoConstraints = NO;
    _viewLine.translatesAutoresizingMaskIntoConstraints = NO;
    
    _imgUserPhoto.translatesAutoresizingMaskIntoConstraints = NO;
    _labelUserName.translatesAutoresizingMaskIntoConstraints = NO;
    _labelTime.translatesAutoresizingMaskIntoConstraints = NO;
    
    _labelTitle.translatesAutoresizingMaskIntoConstraints = NO;
    _imgPhoto.translatesAutoresizingMaskIntoConstraints = NO;
    
    _imgZan.translatesAutoresizingMaskIntoConstraints = NO;
    _labelZan.translatesAutoresizingMaskIntoConstraints = NO;
    _imgComment.translatesAutoresizingMaskIntoConstraints = NO;
    _labelCommentCount.translatesAutoresizingMaskIntoConstraints = NO;
    
    _imgAlert.translatesAutoresizingMaskIntoConstraints = NO;
    
    _labelComment.translatesAutoresizingMaskIntoConstraints = NO;
    _labelSeeAll.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    


}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
